#!/bin/bash
# author tyb
# date 20240217

function isgreen() {
    if [ $? -eq 0 ]; then
        echo -e "\e[32m$1==========>succeed\e[0m"
    else
        echo -e "\e[31m$1==========>failed\e[0m"
        exit 1
    fi
}

function get_sn() {
  sn=$(smartctl -i /dev/"$1" | grep "Serial Number:" | awk  '{print $3}')
  echo "$1: $sn"
  
}

# 收集变量
# while [[ $# -gt 0 ]]; do
#   case "$1" in
#     --help)
#        print_help
#         exit 0
#         ;;
#     --disk|-d)
#       if [[ -n "$2" ]]; then
#         disk_list="$2"
#         shift 2
#       else
#         echo "Missing argument for --help option."
#         exit 1
#       fi
#       ;;
#     *)
#       echo "Invalid option: $1"
#       exit 1
#       ;;
#   esac
# done



# 获取所有块设备列表
block_devices=$(lsblk -o NAME -n -d)

# 遍历每个块设备
for device in $block_devices; do
    # 如果设备路径包含 nvme，则认为是 NVMe 设备
    if [[ $device =~ ^nvme ]]; then
        get_sn "$device"
    else
        # 排除不是磁盘的设备（如分区）
        if [[ $device =~ ^[a-z]+$ ]]; then
            get_sn "$device"
        fi
    fi
done